<template>
  <!-- 存在goodsInfo.goods_name 才渲染页面 -->
  <view v-if="goodsInfo.goods_name" class="goods-detail-container">
    <!-- 商品信息 -->
    <view class="goods-info-box">
      <view class="price">￥{{goodsInfo.goods_price}}</view>
      <!-- 商品主体部分 -->
      <view class="goods-info-body">
        <!-- 商品价格 -->
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <!-- 商品收藏 -->
        <view class="favi">
          <uni-icons type="star" size="20" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 快递 -->
      <view class="delivery">快递：免运费</view>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in goodsInfo.pics" :key="index">
        <!-- 把当前点击的图片的索引，传递到preview()处理函数中 -->
        <image :src="item.pics_big" mode="" @click="preview(index)"></image>
      </swiper-item>
    </swiper>

    <!-- 富文本区域的转换 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>

    <!-- 商品导航区域 -->
    <view class="nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from "vuex"
  export default {
    computed: {
      //添加属性
      ...mapState('m_cart', ["cart"]),
      ...mapGetters('m_cart', ["totalCount"])
    },

    //watch监听器
    watch: {
      // totalCount(newValue) {
      //   //1.先把购物车这个对象取出来
      //   const findResult = this.options.find(item => item.text === '购物车')
      //   //2.如果存在,则把里面的info属性的值变成totalCount的值
      //   if (findResult) {
      //     findResult.info = newValue
      //   }
      // }
      totalCount: {
        handler(newValue) {
          //   //1.先把购物车这个对象取出来
          const findResult = this.options.find(item => item.text === '购物车')
          //   //2.如果存在,则把里面的info属性的值变成totalCount的值

          if (findResult) {
            findResult.info = newValue
          }
        },
        //表明页面一加载完毕就渲染好totalCount
        immediate: true

      }
    },

    data() {
      return {
        //商品详情对象
        goodsInfo: {},
        //商品导航
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(options) {
      console.log('打印一下：');
      console.log(options.goods_id);
      this.getGoodsInfo(options.goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      //获取轮播图图片数据
      async getGoodsInfo(goods_id) {
        const res = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: goods_id
        })
        if (res.data.meta.status !== 200) return uni.showMsg('响应错误')
        //使用字符串的replace()方法，为img 标签添加行内的style样式，从而解决图片底部空白间隙的问题
        //将所有webp后缀的图片变成.jpg后缀，这样真机预览也可以正常显示图片啦
        res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g,
          '<img style="display:block;"').replace(/webp/g, 'jpg')

        //<img style="display:block;"data-src="//image.suning.cn/uimg/sop/commodity/966602987133443585157120_x.jpg?from=mobile&amp;format=80q.webp" alt="" src="//image.suning.cn/uimg/sop/commodity/966602987133443585157120_x.jpg?from=mobile&format=80q.webp" width="100%" height="auto">
        this.goodsInfo = res.data.message
        console.log(this.goodsInfo.goods_introduce);
      },

      //预览轮播图
      preview(index) {
        //调用uni.previewImage()方法预览图片
        uni.previewImage({
          //预览时，默认显示图片的索引
          current: index,
          //将所有图片url地址的数组
          urls: this.goodsInfo.pics.map(x => x.pics_big)
        })
      },
      //导航栏的条数
      buttonClick(e) {
        console.log(e)
        if (e.content.text === '加入购物车') {
          //每个商品的信息对象，都包含如下6个属性：
          //{goods_id,goods_name,goods_price,goods_count,goods_big_logo,goods_state}
          const goods = {
            goods_id: this.goodsInfo.goods_id,
            goods_name: this.goodsInfo.goods_name,
            goods_price: this.goodsInfo.goods_price,
            goods_count: this.goodsInfo.goods_count,
            goods_big_logo: this.goodsInfo.goods_big_logo,
            goods_state: true
          }
          //调用了cart.js中的addToCart（）方法将商品加入购物车列表中
          this.addToCart(goods)
          console.log(goods);

        }
      },
      //点击跳转到购物车页面
      onClick(e) {
        console.log(e.content.text);
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      }
    }

  }
</script>

<style lang="scss">
  swiper {
    height: 560rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    margin-left: 10px;

    .price {
      color: #C00000;
      font-size: 19px;
      font-weight: 900;
    }

    .goods-info-body {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      margin: 5px 0;

      .goods-name {
        width: 78%;
        border-right: 1px solid #bdbdbd;
        padding-right: 10px;

      }

      .favi {
        display: flex;
        color: #bdbdbd;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }
    }

    .delivery {
      color: #bdbdbd;
      font-size: 14px;
      margin: 5px;
    }
  }

  .goods-detail-container {
    //给页面外层的容器，添加50px 的内padding,
    //防止页面内容被底部的商品导航组件遮盖
    padding-bottom: 50px;
  }

  .nav {
    //为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>